var constant = require('../config/constant');
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    service: constant.emailService,
    auth: {
        user: constant.emailAccount,
        pass: constant.emailPwd
    }
});
module.exports = {
	getReloginObj: function(){
		var result = {};
		result.status = constant.reloginNeededStatus;
		result.desc = constant.userNotAuthenticated;
		return result;
	},
	sendResetPwdEmail: function(mailOptions, reset_pwd_token, succCb, errCb){
		var emailFormula = /\$\{email\}/g;
		var tokenFormula = /\$\{reset_pwd_token\}/g;
		var domainFormula = /\$\{domain\}/g;
		mailOptions.html = mailOptions.html.replace(emailFormula, mailOptions.to);
		mailOptions.html = mailOptions.html.replace(tokenFormula, reset_pwd_token);
		mailOptions.html = mailOptions.html.replace(domainFormula, constant.domain);
		this.sendEmail(mailOptions, succCb, errCb);
	},
	sendEmail: function(mailOptions, succCb, errCb){
		transporter.sendMail(mailOptions, function(error, info){
		    if(error){
		        if(errCb){
		        	errCb();
		        }
		    }else{
		        if(succCb){
		        	succCb();
		        }
		    }
		});
	}
}